package servidor;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import Ventanas.VentanaAct;

import ClasesBasicas.Usuario;

import util.SocketManager;

public class Hilo implements Runnable{

	private SocketManager s;
	private String us;
	private VentanaAct ventana;
	
	
	public SocketManager getSocket(){
		return s;
	}
	public String getUsuario(){
		return us;
	}
	public Hilo(SocketManager sm,VentanaAct v){
		ventana=v;
		s=sm;
	}
	
	
	@Override
	public void run() {
		String comando="";
		fachada f= new fachada();
		String c="";
		
		try {
			do{
			c=s.Leer();
			if(c.contains("USER")){
				us = c.substring(5);
			}
			comando=f.executeComand(c);
			
			if(comando.contains("Bienvenido al sistema")){
					ventana.recargar();
			}
			
			
			s.Escribir(comando+"\n");
			}while(!c.equals("SALIR "));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		try {
			ventana.getHilos().remove(this);
			ventana.recargar();
			s.CerrarStreams();
			s.CerrarSocket();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
}
